#ifndef __CLASS_BASELAMP_H__
#define __CLASS_BASELAMP_H__
#include <Arduino.h>
#include <Protothread.hpp>

template <class T>
class BaseLamp
{
protected:
    uint16_t _speed;
    uint32_t _last;
    uint32_t _pin;

    // typedef unsigned short LineNumber;
    // static const LineNumber LineNumberInvalid = (LineNumber)(-1);
    // LineNumber _ptLine;

    typedef void *LineNumber;
    typedef unsigned long TimeoutNumber;
    static constexpr LineNumber LineNumberInvalid = nullptr;
    LineNumber _ptLine;
    TimeoutNumber _ptTime;

public:
    BaseLamp(uint32_t pin)
        : _speed(10), _last(0), _pin(pin), _ptLine(0)
    {
        pinMode(pin, OUTPUT);
    }
    virtual ~BaseLamp() {}

    void Stop()
    {
        _ptLine = LineNumberInvalid;
    }

    void Restart()
    {
        _ptLine = nullptr;
    }

    bool IsRunning()
    {
        return _ptLine != LineNumberInvalid;
    }

    bool Run()
    {
        return cast()->T::Run();
    }

    void setSpeed(uint16_t speed)
    {
        _speed = speed;
    }

protected:
    inline T *cast()
    {
        return static_cast<T *>(this);
    }
    inline const T *cast() const
    {
        return static_cast<const T *>(this);
    }
};

#endif //!__CLASS_BASELAMP_H__